package tree;

/**
 * @author : Rookie
 * @description : 判断一棵树是不是二叉搜索树
 * @date : 2023/10/26
 */
public class CheckBinarySearchTree {

    public static void main(String[] args) {
        int[] input = new int[] {9,8,12,6,-1,11,15};
        TreeNode root = BinaryTreeUtil.buildTree(input);
        System.out.println(isBinarySearchTree(root));
    }

    private static boolean isBinarySearchTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        int element = root.element;
        if (root.left != null && root.left.element > element) {
            return false;
        }
        if (root.right != null && root.right.element < element) {
            return false;
        }
        boolean lFlag = isBinarySearchTree(root.left);
        boolean rFlag = isBinarySearchTree(root.right);
        return lFlag && rFlag;
    }
}
